﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using e_commerce.BLL.Data;
using e_commerce.Models;
using e_commerce.BLL.Services.Enterprise;
using e_commerce.BLL;
using System.Web.Routing;

namespace e_commerce.Filter
{
    public class PermissionFilter:ActionFilterAttribute
    {
        
        private string permission;

        public PermissionFilter(string permission)
        {
            this.permission = permission;
        }

        //public override void OnActionExecuting(ActionExecutingContext filterContext)
        //{
        //    var user = filterContext.HttpContext.Session["ADMIN"] as AdminUser;
        //    if (user == null)
        //    {
        //        SetRouteResult(filterContext);
        //    }
        //    else
        //    {
        //        PermissionsService ps = new PermissionsService();
        //        var permissions = filterContext.HttpContext.Session["Permissions"] as IEnumerable<Permissions>;

        //        var answer = false;
        //        foreach(var i in permissions)
        //        {
        //            if(i.Code == permission) { answer = true; }
        //        }
        //        if (!answer)
        //        {
        //            SetRouteResult(filterContext);
        //        }
        //    }
        //    base.OnActionExecuting(filterContext);
        //}

        public void SetRouteResult(ActionExecutingContext filterContext)
        {
            //重定向到登录界面
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
            {
                controller = "BackAccount",
                action = "Admin"
            }));
        }
    }
}